package com.jdjw.modules.buscomplaint.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdjw.common.PageCondition;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.buscommentjcllzzfw.entity.BusCommentJcllzzfw;
import com.jdjw.modules.buscommentjcllzzfw.entity.QueryBusCommentJcllzzfw;
import com.jdjw.modules.buscommentjcllzzfw.service.BusCommentJcllzzfwService;
import com.jdjw.modules.buscomplaint.entity.BusComplaint;
import com.jdjw.modules.buscomplaint.entity.BusComplaintVo;
import com.jdjw.modules.buscomplaint.service.BusComplaintService;
import com.jdjw.modules.buscomplaintaccept.entity.BusComplaintAccept;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 群众投诉表 前端控制器
 * </p>
 *
 * @author fq
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/buscomplaint")
public class BusComplaintController {

    @Resource
    BusComplaintService busComplaintService;

    @ApiOperation("监督一点通-群众投诉-添加")
    @PostMapping("addComplaintInfo")
    public ResponseResult addComplaintInfo(BusComplaintVo complaintVo) {
        BusComplaint busComplaint = new BusComplaint();
//        String orgId = busComplaintService.getorgId(complaintVo.getStreet());
//        String deptId = busComplaintService.getDeptId(complaintVo.getCommunity(), orgId);
//        complaintVo.setOrgId(orgId);
//        complaintVo.setDeptId(deptId);
        BeanUtils.copyProperties(complaintVo, busComplaint);
        System.out.println(busComplaint);
        busComplaint.setOrgId(complaintVo.getOrg_id());
        busComplaint.setDeptId(complaintVo.getDept_id());
        System.out.println(busComplaint);
        return busComplaintService.addComplaintInfo(busComplaint);
    }

    @ResponseBody
    @RequestMapping(value = "{reportId}/constructLicensePage", method = RequestMethod.POST)
    @ApiOperation("个人投诉分页查询")
    public ResponseResult constructLicensePage(@ApiParam(value = "投诉用户唯一标识id", required = true) @PathVariable("reportId") String reportId,
                                                    @RequestBody PageCondition queryEntity) {
        ResponseResult result = new ResponseResult();
        try{
            QueryWrapper<BusComplaint> wrapper = new QueryWrapper();
            wrapper.eq("report_id", reportId);
            Map<String, Object> map = new HashMap<>();

            Page<BusComplaint> busComplaintPage = new Page<>(queryEntity.getPageNo(), queryEntity.getPageSize());
            IPage<BusComplaint> page = busComplaintService.page(busComplaintPage, wrapper);
            //map.put("count", page.getTotal());
            //map.put("data", page.getRecords());
            map.put("items", page.getRecords());
            map.put("total", page.getTotal());

            result.setCode(200);
            result.setMsg("SUCCESS");
            result.setData(map);
        }
        catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("ERROR");
            result.setData("请求失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "{id}/getInfo", method = RequestMethod.POST)
    @ApiOperation("投诉详情")
    public ResponseResult getById(@ApiParam(value = "投诉数据id", required = true) @PathVariable("id") String id) {
        return busComplaintService.getComplaintData(id);
    }

    @ApiOperation("群众投诉-处理评价")
    @PostMapping("{id}/complaintAcceptComment")
    public ResponseResult complaintAcceptComment(@ApiParam(value = "投诉数据id", required = true) @PathVariable("id") String id,
                                                 String commentGrade, String commentText) {
        ResponseResult result = new ResponseResult();
        try{
            BusComplaint busComplaint = new BusComplaint();
            busComplaint.setId(id);
            busComplaint.setCommentGrade(commentGrade);
            busComplaint.setCommentText(commentText);
            busComplaint.setCommentTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            busComplaint.setIfComment("1");
            busComplaintService.updateById(busComplaint);

            result.setCode(200);
            result.setMsg("SUCCESS");
            result.setData("评价成功");
        }
        catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("ERROR");
            result.setData("评价失败");
        }
        return result;
    }

}

